package com.cloudm.technician.ui.modules.cloudm_box.channel_list

import android.content.Intent
import android.os.Bundle
import com.cloudm.technician.data.model.ChannelInfo
import com.cloudm.technician.data.model.DeviceInfo
import com.cloudm.technician.extensions.openActivity
import com.cloudm.technician.ui.base.BaseAdapter
import com.cloudm.technician.ui.base.BaseListFragment
import com.cloudm.technician.ui.modules.cloudm_box.transfer.TransferActivity
import com.cloudm.technician.ui.modules.cloudm_box.upload_pic_token.UploadPicTokenActivity

/**
 * 作者: wesley
 * 时间: 2017/12/23
 * 描述: ui
 */
class ChannelListFragment : BaseListFragment<ChannelListPresenter, ChannelInfo>(),
        ChannelListContract.View {

    private var info: DeviceInfo? = null

    override val dividerEnabled: Boolean
        get() = false

    companion object {

        fun newInstance(info: DeviceInfo?) = ChannelListFragment().apply {
            arguments = Bundle().apply {
                putParcelable("info", info)
            }
        }
    }

    override fun onItemClick(t: ChannelInfo) {
        presenter?.openActivity(t)
    }

    override fun createAdapter(): BaseAdapter<ChannelInfo> = ChannelListAdapter()

    override fun loadData() {
        presenter?.getChannelInfo(startPage, PAGE_SIZE, info?.id ?: -1)
    }

    override fun initInject() {
        DaggerChannelListComponent.builder().appComponent(appComponent).channelListPresenterModule(
                ChannelListPresenterModule(this)).build().inject(this)
    }

    override fun handleBundle(bundle: Bundle) {
        info = bundle.getParcelable("info")
    }

    override fun openUploadPicTokenActivity(id: String) {
        val intent = Intent(mContext, UploadPicTokenActivity::class.java).apply {
            putExtras(Bundle().apply {
                putParcelable("info", info)
                putString("channelId", id)
            })
        }
        mContext.openActivity(intent)
    }

    override fun openTransferActivity(id: String?) {
        val intent = Intent(mContext, TransferActivity::class.java).apply {
            putExtras(Bundle().apply {
                putParcelable("info", info)
                putString("channelId", id)
            })
        }
        mContext.openActivity(intent)
    }

    override fun finishSelf() {
        val activity = mContext as? ChannelListActivity ?: return
        activity.finish()
    }
}